﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security;
using System.Web.Security;
using System.Configuration.Provider;
using Comsenz.Base.Exceptions;

namespace Comsenz.Discuz.Providers
{
    /// <summary>
    /// Discuz角色提供器
    /// </summary>
    /// <author>Chiron</author>
    public class DiscuzRoleProvider : RoleProvider
    {
        /// <summary>
        /// 添加用户到角色
        /// </summary>
        /// <param name="usernames">用户集</param>
        /// <param name="roleNames">角色集</param>
        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// 程序名
        /// </summary>
        public override string ApplicationName
        {
            get
            {
                return appName;
            }
            set
            {
                appName = value;
                if (string.IsNullOrEmpty(appName) && appName.Length > 0x100)
                {
                    throw new ProviderException("程序名长度有误!");
                }
            }
        }
        private string appName;
        /// <summary>
        /// 创建角色
        /// </summary>
        /// <param name="roleName">角色名</param>
        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="roleName">角色名</param>
        /// <param name="throwOnPopulatedRole"></param>
        /// <returns></returns>
        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }
        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            try
            {
                return DiscuzRoleManager.GetAllRoles();
            }
            catch (ServiceIsClosedException)
            {
                return new string[] { };
            }
        }

        public override string[] GetRolesForUser(string username)
        {
            try
            {
                return DiscuzRoleManager.GetRolesForUser(username);
            }
            catch (ServiceIsClosedException)
            {
                return new string[] { };
            }
        }

        public override string[] GetUsersInRole(string roleName)
        {
            try
            {
                return DiscuzRoleManager.GetUsersInRole(roleName);
            }
            catch (ServiceIsClosedException)
            {
                return new string[] { };
            }
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            try
            {
                return DiscuzRoleManager.IsUserInRole(username, roleName);
            }
            catch (ServiceIsClosedException)
            {
                return false;
            }
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            try
            {
                return DiscuzRoleManager.RoleExists(roleName);
            }
            catch (ServiceIsClosedException)
            {
                return false;
            }
        }
    }
}
